/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.packager.gui.helpers;

import javax.swing.table.AbstractTableModel;

import sshtunneler.packager.data.InstalledTools;
import sshtunneler.tools.Tool;

public class ToolTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 737070223150343807L;
	private String[] columnNames = { "Tool name", "Included ?" };

	public int getColumnCount() {
		return this.columnNames.length;
	}

	public int getRowCount() {
		return InstalledTools.inst.nb_tools();
	}

	@Override
	public String getColumnName(int col) {
		return this.columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		if (row > this.getRowCount()) {
			return null;
		}
		Tool selected = InstalledTools.inst.get_tool(row);
		if (selected == null) {
			return null;
		}
		switch (col) {
		case 0:
			return selected.getName();
		case 1:
			return InstalledTools.inst.tool_installed(selected);
		}
		return null;
	}

	@Override
	public Class<?> getColumnClass(int c) {
		return this.getValueAt(0, c).getClass();
	}

	@Override
	public boolean isCellEditable(int row, int col) {
		if (col == 1) {
			return true;
		}
		return false;
	}

	@Override
	public void setValueAt(Object value, int row, int col) {
		if (col != 1 || !(value instanceof Boolean)) {
			return;
		}

		if (row > this.getRowCount()) {
			return;
		}
		Tool selected = InstalledTools.inst.get_tool(row);
		if (selected == null) {
			return;
		}

		InstalledTools.inst.set_tool(selected, (Boolean) value);
		this.fireTableCellUpdated(row, col);
	}
}
